package com.coc.service;

import com.coc.bean.*;
import com.coc.bean.def.*;
import com.coc.bean.res.*;
import com.coc.bean.war.*;
import com.coc.dao.*;
import com.coc.util.*;
import java.util.*;

import javax.servlet.http.*;

import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class BaseService {
	//检测玩家升级情况
	public boolean checkUserLV(CcUser cu , HttpServletRequest request) {
		boolean lvup = false;
		
		return lvup;
	}
	
	//检测全部建筑物的升级情况
	public void checkAllUpgradeBuilding(CcMap cm, long t, HttpServletRequest request) {
		
	}
	
	// 初始化魔法生产
	// 魔法工厂1次只能研究一个魔法,本方法检测当前研究队列是否完成
	// 完成放入完工队列
	public void initSpell(CcMap cm, long t, HttpServletRequest request) {
		Spel spel = cm.getSpel();
		// 没就返回
		if (spel == null || spel.getResearching().length() < 1) {
			return;
		}
		String type = spel.getResearching();
		Troops tr = getBuildSettingXML(request).findTrByID(type);
		// 时间完工时间
		long donetime = spel.getUpgradeEnd() + tr.getTime();
		if (t >= donetime) {// 完工
			boolean haved = false;
			String done = spel.getCurspell();
			String[] speling = done.split(",");
			for (int i = 0; i < speling.length; i++) {
				String tmp_type = speling[i].substring(0,
						speling[i].length() - 2);
				int count = Integer.parseInt(speling[i].substring(speling[i]
						.length() - 1));
				if (tmp_type.equals(type)) {
					haved = true;
					done = done.replaceAll(speling[i], tmp_type + "|"
							+ (count + 1));
					i = 10;
					continue;
				}
			}
			// 新的
			if (!haved) {
				if (done.length() < 1) {
					done = type + "|1";
				} else {
					done = done + "," + type + "|1";
				}
			}
			spel.setResearching("");
			spel.setResearchbeg(0);
			spel.setCurspell(done);

		}
		cm.setSpel(spel);
	}

	// 考虑改成单兵营?
	// 遍历兵营,造好的兵放入军营
	public void initAllTrain(CcMap cm, long t, HttpServletRequest request) {
		// 全局完工队列
		List<String> donelist = new ArrayList<String>();
		// 逐兵营循环是否完工
		for (int i = 0; i < cm.getBarks().size(); i++) {
			Tran tran = (Tran) cm.getBarks().get(i);
			long begtime = tran.getProcessbeg();
			// 当前建造队列
			String[] units = tran.getProcess().split(",");
			// 单兵营完工队列
			List<String> subdonelist = new ArrayList<String>();
			// 逐兵种循环是否完工
			for (int j = 0; j < units.length; j++) {
				if (units[j].length() > 1) {
					String tmp_type = units[j].substring(0, units[j]
							.indexOf("|"));
					int count = Integer.parseInt(units[j].substring(units[j]
							.indexOf("|") + 1));
					Troops trs = getBuildSettingXML(request).findTrByID(
							tmp_type);
					// 逐个循环是否完工
					for (int z = 0; z < count; z++) {
						long needtime = trs.getTime().intValue() * 1000L;
						// if ((begtime > tran.getBoosttime())
						// && (begtime < tran.getBoosttime2())) {
						// needtime /= 2L;
						// }
						long donetime = begtime + needtime;
						// 完工时间大于t,跳出单兵营循环
						if (donetime > t) {
							j = 1000;
							z = 1000;
						} else {
							begtime = donetime;
							donelist.add(tmp_type);
							subdonelist.add(tmp_type);
						}
					}
				}
			}
			// 更新单兵营
			tran.setProcessbeg(begtime);
			String process = tran.getProcess();
			// 把process中数据移除掉生产好的部分
			for (int j = 0; j < subdonelist.size(); j++) {
				int pos = process.indexOf((String) subdonelist.get(i));
				int curcount = Integer.parseInt(process.substring(pos
						+ ((String) subdonelist.get(j)).length() + 1, pos
						+ ((String) subdonelist.get(j)).length() + 2));
				if (curcount > 1)
					process = process.replace((String) subdonelist.get(j) + "|"
							+ curcount, (String) subdonelist.get(j) + "|"
							+ (curcount - 1));
				else {
					process = process.replace((String) subdonelist.get(j) + "|"
							+ curcount, "");
				}
			}
			// 整理process的字符串,去掉多余的,
			checkRes(process);
			tran.setProcess(process);
			// 队列空了
			if (tran.getProcess().length() < 1) {
				tran.setProcessbeg(0);
			}
			cm.getBarks().set(i, tran);
		}
		for (int i = 0; i < cm.getBkdks().size(); i++) {
			Trdk tran = (Trdk) cm.getBkdks().get(i);
			long begtime = tran.getProcessbeg();
			String[] units = tran.getProcess().split(",");

			List<String> subdonelist = new ArrayList<String>();

			for (int j = 0; j < units.length; j++) {
				if (units[j].length() > 1) {
					String tmp_type = units[j].substring(0, units[j]
							.indexOf("|"));
					int count = Integer.parseInt(units[j].substring(units[j]
							.indexOf("|") + 1));
					Troops trs = getBuildSettingXML(request).findTrByID(
							tmp_type);

					for (int z = 0; z < count; z++) {
						long needtime = trs.getTime().intValue() * 1000L;
						// if ((begtime > tran.getBoosttime())
						// && (begtime < tran.getBoosttime2())) {
						// needtime /= 2L;
						// }
						long donetime = begtime + needtime;

						if (donetime > t) {
							j = 100;
							z = 100;
						} else {
							begtime = donetime;
							donelist.add(tmp_type);
							subdonelist.add(tmp_type);
						}
					}

				}

			}

			tran.setProcessbeg(begtime);
			String process = tran.getProcess();
			for (int j = 0; j < subdonelist.size(); j++) {
				int pos = process.indexOf((String) subdonelist.get(j));
				int curcount = Integer.parseInt(process.substring(pos
						+ ((String) subdonelist.get(j)).length() + 1, pos
						+ ((String) subdonelist.get(j)).length() + 2));
				if (curcount > 1)
					process = process.replace((String) subdonelist.get(j) + "|"
							+ curcount, (String) subdonelist.get(j) + "|"
							+ (curcount - 1));
				else {
					process = process.replace((String) subdonelist.get(j) + "|"
							+ curcount, "");
				}
			}
			checkRes(process);
			tran.setProcess(process);

			if (tran.getProcess().length() < 1) {
				tran.setProcessbeg(0);
			}
			cm.getBkdks().set(i, tran);
		}
		// 军营的完工列表,要加上现在新增的完工列表
		String done = cm.getCamplist();
		for (int i = 0; i < donelist.size(); i++) {
			if (done.length() == 0) {
				done = (String) donelist.get(i) + "|1";
			} else {
				int pos = done.indexOf((String) donelist.get(i));
				if (pos == -1) {
					done = done + "," + (String) donelist.get(i) + "|1";
				} else {
					int curcount = Integer.parseInt(done.substring(pos
							+ ((String) donelist.get(i)).length() + 1, pos
							+ ((String) donelist.get(i)).length() + 2));
					done = done.replace((String) donelist.get(i) + "|"
							+ curcount, (String) donelist.get(i) + "|"
							+ (curcount + 1));
				}
			}
		}
		cm.setCamplist(done);
	}

	//检测单个大类(Type)建筑物的升级情况
	//如果是hall升级完成.要更新ccuser和ceu的htlv,
	public void checkUpgradeBuilding(CcMap cm, long t,String type,String s_exp, HttpServletRequest request) {
		int exp = Integer.parseInt(s_exp.length()==0?"0":s_exp);
		CcEloUser ceu = getEloUserDAO(request).findByID(cm.getUid());
		CcUser cu = cm.getCcuser();
		//DECT特别处理,不用改lv,直接处理exp部分
		//HALL,CAST等,cm中只有唯一一个的,可以直接lv+1
		//WALL,CANN之类是个队列,不知道是哪个升级好了,直接遍历list
		if (type.length() < 4 && type.startsWith("D")) {
			Hashtable<String, List<Dect>> list = cm.getDects();
			List<Dect> dects = list.get(type);
		}else if(type.equals("HALL")){
			cm.getHall().setLv(cm.getHall().getLv()+1);
			cm.getHall().setUpgradeBeg(0L);
			cm.getHall().setUpgradeEnd(0L);
			cu.setHtlv(cm.getHall().getLv());
			ceu.setHtlv(cm.getHall().getLv());
			getEloUserDAO(request).saveOrUpdate(ceu);
		}else if(type.equals("WALL")){
			List<Wall> list = cm.getWalls();
			for(int i=0;i<list.size();i++){
				//升级结束
				if(list.get(i).getUpgradeEnd()<=t){
					list.get(i).setLv(list.get(i).getLv()+1);
					list.get(i).setUpgradeBeg(0L);
					list.get(i).setUpgradeEnd(0L);
				}
			}
			cm.setWalls(list);
		}else if(type.equals("CAST")){
			cm.getCast().setLv(cm.getCast().getLv()+1);
			cm.getCast().setUpgradeBeg(0L);
			cm.getCast().setUpgradeEnd(0L);
		}else if(type.equals("LABT")){
			cm.getLabt().setLv(cm.getLabt().getLv()+1);
			cm.getLabt().setUpgradeBeg(0L);
			cm.getLabt().setUpgradeEnd(0L);
		}else if(type.equals("SPEL")){
			cm.getSpel().setLv(cm.getSpel().getLv()+1);
			cm.getSpel().setUpgradeBeg(0L);
			cm.getSpel().setUpgradeEnd(0L);
		}else if(type.equals("KING")){
			cm.getKing().setLv(cm.getKing().getLv()+1);
			cm.getKing().setUpgradeBeg(0L);
			cm.getKing().setUpgradeEnd(0L);
		}else if(type.equals("QUEN")){
			cm.getQuen().setLv(cm.getQuen().getLv()+1);
			cm.getQuen().setUpgradeBeg(0L);
			cm.getQuen().setUpgradeEnd(0L);
		}else if(type.equals("ELXM")){
			List<Elxm> list = cm.getElxms();
			for(int i=0;i<list.size();i++){
				//升级结束
				if(list.get(i).getUpgradeEnd()<=t){
					list.get(i).setLv(list.get(i).getLv()+1);
					list.get(i).setUpgradeBeg(0L);
					list.get(i).setUpgradeEnd(0L);
				}
			}
			cm.setElxms(list);
		}else if(type.equals("ELXS")){
			List<Elxs> list = cm.getElxss();
			for(int i=0;i<list.size();i++){
				//升级结束
				if(list.get(i).getUpgradeEnd()<=t){
					list.get(i).setLv(list.get(i).getLv()+1);
					list.get(i).setUpgradeBeg(0L);
					list.get(i).setUpgradeEnd(0L);
				}
			}
			cm.setElxss(list);
		}else if(type.equals("GOLM")){
			List<Golm> list = cm.getGolms();
			for(int i=0;i<list.size();i++){
				//升级结束
				if(list.get(i).getUpgradeEnd()<=t){
					list.get(i).setLv(list.get(i).getLv()+1);
					list.get(i).setUpgradeBeg(0L);
					list.get(i).setUpgradeEnd(0L);
				}
			}
			cm.setGolms(list);
		}else if(type.equals("GOLS")){
			List<Gols> list = cm.getGolss();
			for(int i=0;i<list.size();i++){
				//升级结束
				if(list.get(i).getUpgradeEnd()<=t){
					list.get(i).setLv(list.get(i).getLv()+1);
					list.get(i).setUpgradeBeg(0L);
					list.get(i).setUpgradeEnd(0L);
				}
			}
			cm.setGolss(list);
		}else if(type.equals("DAKM")){
			List<Dakm> list = cm.getDakms();
			for(int i=0;i<list.size();i++){
				//升级结束
				if(list.get(i).getUpgradeEnd()<=t){
					list.get(i).setLv(list.get(i).getLv()+1);
					list.get(i).setUpgradeBeg(0L);
					list.get(i).setUpgradeEnd(0L);
				}
			}
			cm.setDakms(list);
		}else if(type.equals("DAKS")){
			List<Daks> list = cm.getDakss();
			for(int i=0;i<list.size();i++){
				//升级结束
				if(list.get(i).getUpgradeEnd()<=t){
					list.get(i).setLv(list.get(i).getLv()+1);
					list.get(i).setUpgradeBeg(0L);
					list.get(i).setUpgradeEnd(0L);
				}
			}
			cm.setDakss(list);
		}else if(type.equals("CAMP")){
			List<Camp> list = cm.getCamps();
			for(int i=0;i<list.size();i++){
				//升级结束
				if(list.get(i).getUpgradeEnd()<=t){
					list.get(i).setLv(list.get(i).getLv()+1);
					list.get(i).setUpgradeBeg(0L);
					list.get(i).setUpgradeEnd(0L);
				}
			}
			cm.setCamps(list);
		}else if(type.equals("BARK")){
			List<Tran> list = cm.getBarks();
			for(int i=0;i<list.size();i++){
				//升级结束
				if(list.get(i).getUpgradeEnd()<=t){
					list.get(i).setLv(list.get(i).getLv()+1);
					list.get(i).setUpgradeBeg(0L);
					list.get(i).setUpgradeEnd(0L);
				}
			}
			cm.setBarks(list);
		}else if(type.equals("BKDK")){
			List<Trdk> list = cm.getBkdks();
			for(int i=0;i<list.size();i++){
				//升级结束
				if(list.get(i).getUpgradeEnd()<=t){
					list.get(i).setLv(list.get(i).getLv()+1);
					list.get(i).setUpgradeBeg(0L);
					list.get(i).setUpgradeEnd(0L);
				}
			}
			cm.setBkdks(list);
		}else if(type.equals("CANN")){
			List<Cann> list = cm.getCanns();
			for(int i=0;i<list.size();i++){
				//升级结束
				if(list.get(i).getUpgradeEnd()<=t){
					list.get(i).setLv(list.get(i).getLv()+1);
					list.get(i).setUpgradeBeg(0L);
					list.get(i).setUpgradeEnd(0L);
				}
			}
			cm.setCanns(list);
		}else if(type.equals("ARCT")){
			List<Arct> list = cm.getArcts();
			for(int i=0;i<list.size();i++){
				//升级结束
				if(list.get(i).getUpgradeEnd()<=t){
					list.get(i).setLv(list.get(i).getLv()+1);
					list.get(i).setUpgradeBeg(0L);
					list.get(i).setUpgradeEnd(0L);
				}
			}
			cm.setArcts(list);
		}else if(type.equals("MORT")){
			List<Mort> list = cm.getMorts();
			for(int i=0;i<list.size();i++){
				//升级结束
				if(list.get(i).getUpgradeEnd()<=t){
					list.get(i).setLv(list.get(i).getLv()+1);
					list.get(i).setUpgradeBeg(0L);
					list.get(i).setUpgradeEnd(0L);
				}
			}
			cm.setMorts(list);
		}else if(type.equals("WIZT")){
			List<Wizt> list = cm.getWizts();
			for(int i=0;i<list.size();i++){
				//升级结束
				if(list.get(i).getUpgradeEnd()<=t){
					list.get(i).setLv(list.get(i).getLv()+1);
					list.get(i).setUpgradeBeg(0L);
					list.get(i).setUpgradeEnd(0L);
				}
			}
			cm.setWizts(list);
		}else if(type.equals("AIRD")){
			List<Aird> list = cm.getAirds();
			for(int i=0;i<list.size();i++){
				//升级结束
				if(list.get(i).getUpgradeEnd()<=t){
					list.get(i).setLv(list.get(i).getLv()+1);
					list.get(i).setUpgradeBeg(0L);
					list.get(i).setUpgradeEnd(0L);
				}
			}
			cm.setAirds(list);
		}else if(type.equals("TSLA")){
			List<Hide> list = cm.getTslas();
			for(int i=0;i<list.size();i++){
				//升级结束
				if(list.get(i).getUpgradeEnd()<=t){
					list.get(i).setLv(list.get(i).getLv()+1);
					list.get(i).setUpgradeBeg(0L);
					list.get(i).setUpgradeEnd(0L);
				}
			}
			cm.setTslas(list);
		}else if(type.equals("XBOW")){
			List<Xbow> list = cm.getXbows();
			for(int i=0;i<list.size();i++){
				//升级结束
				if(list.get(i).getUpgradeEnd()<=t){
					list.get(i).setLv(list.get(i).getLv()+1);
					list.get(i).setUpgradeBeg(0L);
					list.get(i).setUpgradeEnd(0L);
				}
			}
			cm.setXbows(list);
		}else if(type.equals("INFT")){
			List<Inft> list = cm.getInfts();
			for(int i=0;i<list.size();i++){
				//升级结束
				if(list.get(i).getUpgradeEnd()<=t){
					list.get(i).setLv(list.get(i).getLv()+1);
					list.get(i).setUpgradeBeg(0L);
					list.get(i).setUpgradeEnd(0L);
				}
			}
			cm.setInfts(list);
		}
		cu.setExp(cu.getExp()+exp);
		//检测是否升级,如果升级,cu.lv已经在checkUserLV+1,这边更新ceu
		if(this.checkUserLV(cu, request)){
			ceu.setLv(cu.getLv());
			getEloUserDAO(request).saveOrUpdate(ceu);
		}
		cm.setCcuser(cu);
	}

	public void checkRes(String res) {
		if (res.startsWith(",")) {
			res = res.substring(1);
		}
		if (res.endsWith(",")) {
			res = res.substring(0, res.length() - 1);
		}
		res = res.replaceAll(",,", ",");
	}

	// 当矿脉进行操作时候(主动收取,加速,被打),容量产生变化
	// 矿脉保存一个回退时间,既是这个时间开始算,到t时刻生产的资源,正好是剩余容量
	public int getLeftTime(HttpServletRequest request, long t, String type,
			Object obj, int leftValue) {
		Building bu = getBuildSettingXML(request).findBuByID(type);
		if (type.equals("GOLM")) {
			Golm tmp = (Golm) obj;
			String[] strs = ((String) bu.getLv().get(tmp.getLv() - 1))
					.split(";");
			// 类型;需求等级(HT/User);花费;建造时间;矿脉容量/dect总数;生产速率;建造经验;加速费用;
			float speed = Float.parseFloat(strs[5]) / 3600.0F;
			int totaltime = (int) (leftValue / speed);
			// t时刻有没加速 -->有,算出加速开始-->t时刻时间差,乘speed.得出双倍生产最高值
			if (tmp.getBoosttime2() >= t) {
				int boosttime = (int) ((t - tmp.getBoosttime()) / 1000);
				int maxboostvalue = (int) (boosttime * speed * 2);
				// 如果值够回扣数量,直接用加速speed算回扣时间
				if (maxboostvalue >= leftValue) {
					return totaltime / 2;
				} else {
					// 如果值不够回扣数量,第一部分时间等于加速时间.剩余回扣值=回扣-加速数量,剩余回扣计算没加速speed,得出第二部分时间
					int lefttime = (int) ((leftValue - maxboostvalue) / speed);
					return (lefttime + boosttime);
				}
			}
			// t时刻有没加速 -->没有 直接用speed算出回扣时间,回扣起点如果也没加速,pass

			else {
				// 直接用speed算出回扣时间,回扣起点如果也没加速,pass
				long leftbeg = t - totaltime * 1000L;
				if (leftbeg >= tmp.getBoosttime2()) {
					return totaltime;
				} else {
					// 回扣起点有加速 回扣时间=回扣时间-回扣加速时长/2
					int boosttime = (int) ((tmp.getBoosttime2() - leftbeg) / 1000);
					return (totaltime - boosttime / 2);
				}
			}
		} else if (type.equals("ELXM")) {
			Elxm tmp = (Elxm) obj;
			String[] strs = ((String) bu.getLv().get(tmp.getLv() - 1))
					.split(";");
			// 类型;需求等级(HT/User);花费;建造时间;矿脉容量/dect总数;生产速率;建造经验;加速费用;
			float speed = Float.parseFloat(strs[5]) / 3600.0F;
			int totaltime = (int) (leftValue / speed);
			// t时刻有没加速 -->有,算出加速开始-->t时刻时间差,乘speed.得出双倍生产最高值
			if (tmp.getBoosttime2() >= t) {
				int boosttime = (int) ((t - tmp.getBoosttime()) / 1000);
				int maxboostvalue = (int) (boosttime * speed * 2);
				// 如果值够回扣数量,直接用加速speed算回扣时间
				if (maxboostvalue >= leftValue) {
					return totaltime / 2;
				} else {
					// 如果值不够回扣数量,第一部分时间等于加速时间.剩余回扣值=回扣-加速数量,剩余回扣计算没加速speed,得出第二部分时间
					int lefttime = (int) ((leftValue - maxboostvalue) / speed);
					return (lefttime + boosttime);
				}
			}
			// t时刻有没加速 -->没有 直接用speed算出回扣时间,回扣起点如果也没加速,pass
			// 回扣起点有加速 回扣时间=回扣时间-回扣加速时长/2
			else {
				// 直接用speed算出回扣时间,回扣起点如果也没加速,pass
				long leftbeg = t - totaltime * 1000L;
				if (leftbeg >= tmp.getBoosttime2()) {
					return totaltime;
				} else {
					int boosttime = (int) ((tmp.getBoosttime2() - leftbeg) / 1000);
					return (totaltime - boosttime / 2);
				}
			}
		} else if (type.equals("DAKM")) {
			Dakm tmp = (Dakm) obj;
			String[] strs = ((String) bu.getLv().get(tmp.getLv() - 1))
					.split(";");
			// 类型;需求等级(HT/User);花费;建造时间;矿脉容量/dect总数;生产速率;建造经验;加速费用;
			float speed = Float.parseFloat(strs[5]) / 3600.0F;
			int totaltime = (int) (leftValue / speed);
			// t时刻有没加速 -->有,算出加速开始-->t时刻时间差,乘speed.得出双倍生产最高值
			if (tmp.getBoosttime2() >= t) {
				int boosttime = (int) ((t - tmp.getBoosttime()) / 1000);
				int maxboostvalue = (int) (boosttime * speed * 2);
				// 如果值够回扣数量,直接用加速speed算回扣时间
				if (maxboostvalue >= leftValue) {
					return totaltime / 2;
				} else {
					// 如果值不够回扣数量,第一部分时间等于加速时间.剩余回扣值=回扣-加速数量,剩余回扣计算没加速speed,得出第二部分时间
					int lefttime = (int) ((leftValue - maxboostvalue) / speed);
					return (lefttime + boosttime);
				}
			}
			// t时刻有没加速 -->没有 直接用speed算出回扣时间,回扣起点如果也没加速,pass
			// 回扣起点有加速 回扣时间=回扣时间-回扣加速时长/2
			else {
				// 直接用speed算出回扣时间,回扣起点如果也没加速,pass
				long leftbeg = t - totaltime * 1000L;
				if (leftbeg >= tmp.getBoosttime2()) {
					return totaltime;
				} else {
					int boosttime = (int) ((tmp.getBoosttime2() - leftbeg) / 1000);
					return (totaltime - boosttime / 2);
				}
			}
		}
		return 0;
	}

	// 取某个金矿或者紫水,黑水矿的生产额
	public int getProductValue(HttpServletRequest request, long t, String type,
			CcBuilding obj) {
		Building bu = getBuildSettingXML(request).findBuByID(type);
		if (type.equals("GOLM")) {
			Golm tmp = (Golm) obj;
			String[] strs = ((String) bu.getLv().get(tmp.getLv() - 1))
					.split(";");
			// 类型;需求等级(HT/User);花费;建造时间;矿脉容量/dect总数;生产速率;建造经验;加速费用;
			int cap = Integer.parseInt(strs[4]);
			float speed = Float.parseFloat(strs[5]) / 3600.0F;
			// 完整的时间*speed+加速时间*(加速speed-speed)
			int noboosttime = (int) ((t - tmp.getLefttime()) / 1000);
			int boostrate = bu.getBoostrate();
			int boostend = 0;
			int booststr = 0;
			// 结束时间大于等于t,boostend=t
			if (tmp.getBoosttime2() >= t) {
				boostend = (int) (t / 1000);
			} else {
				boostend = (int) (tmp.getBoosttime2() / 1000);
			}
			// 开始时间小于于等于t,booststr=Lefttime
			if (tmp.getBoosttime() <= tmp.getLefttime()) {
				booststr = (int) (tmp.getLefttime() / 1000);
			} else {
				booststr = (int) (tmp.getBoosttime() / 1000);
			}
			int boosttime = boostend - booststr;
			if (boosttime < 0) {
				boosttime = 0;
			}
			int value = (int) (noboosttime * speed + (boostrate - 1)
					* boosttime * speed);
			// 本次生产额不超过cap
			if (value > cap) {
				value = cap;
			}
			return value;
		}
		if (type.equals("ELXM")) {
			Elxm tmp = (Elxm) obj;

			String[] strs = ((String) bu.getLv().get(tmp.getLv() - 1))
					.split(";");
			float speed = Float.parseFloat(strs[5]) / 3600.0F;
			int cap = Integer.parseInt(strs[4]);
			// 完整的时间*speed+加速时间*(加速speed-speed)
			int noboosttime = (int) ((t - tmp.getLefttime()) / 1000);
			int boostrate = bu.getBoostrate();
			int boostend = 0;
			int booststr = 0;
			// 结束时间大于等于t,boostend=t
			if (tmp.getBoosttime2() >= t) {
				boostend = (int) (t / 1000);
			} else {
				boostend = (int) (tmp.getBoosttime2() / 1000);
			}
			// 开始时间小于于等于t,booststr=Lefttime
			if (tmp.getBoosttime() <= tmp.getLefttime()) {
				booststr = (int) (tmp.getLefttime() / 1000);
			} else {
				booststr = (int) (tmp.getBoosttime() / 1000);
			}
			int boosttime = boostend - booststr;
			if (boosttime < 0) {
				boosttime = 0;
			}
			int value = (int) (noboosttime * speed + (boostrate - 1)
					* boosttime * speed);
			// 本次生产额不超过cap
			if (value > cap) {
				value = cap;
			}
			return value;
		}

		Dakm tmp = (Dakm) obj;

		String[] strs = ((String) bu.getLv().get(tmp.getLv() - 1)).split(";");
		float speed = Float.parseFloat(strs[5]) / 3600.0F;
		int cap = Integer.parseInt(strs[4]);
		// 完整的时间*speed+加速时间*(加速speed-speed)
		int noboosttime = (int) ((t - tmp.getLefttime()) / 1000);
		int boostrate = bu.getBoostrate();
		int boostend = 0;
		int booststr = 0;
		// 结束时间大于等于t,boostend=t
		if (tmp.getBoosttime2() >= t) {
			boostend = (int) (t / 1000);
		} else {
			boostend = (int) (tmp.getBoosttime2() / 1000);
		}
		// 开始时间小于于等于t,booststr=Lefttime
		if (tmp.getBoosttime() <= tmp.getLefttime()) {
			booststr = (int) (tmp.getLefttime() / 1000);
		} else {
			booststr = (int) (tmp.getBoosttime() / 1000);
		}
		int boosttime = boostend - booststr;
		if (boosttime < 0) {
			boosttime = 0;
		}
		int value = (int) (noboosttime * speed + (boostrate - 1) * boosttime
				* speed);
		// 本次生产额不超过cap
		if (value > cap) {
			value = cap;
		}
		return value;
	}

	public String sortPostData(String data) {
		String[] _array = data.split("&");
		Arrays.sort(_array);
		String _data = "";
		for (String _param : _array)
			_data = _data + "&" + _param;
		return _data.substring(1);
	}

	public static String sortPostData_qq(String data) {
		String[] _array = data.split("&");
		Arrays.sort(_array);
		String _data = "";
		for (String _param : _array) {
			_data = _data + "&" + _param;
		}
		return _data.substring(0);
	}

	public String doHttpClientGet(String url) {
		String str = "";
		try {
			HttpClient httpClient = new HttpClient();
			GetMethod getMethod = new GetMethod(url);
			httpClient.executeMethod(getMethod);
			str = getMethod.getResponseBodyAsString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	public String getParamValue(String key, JSONObject jo,
			HttpServletRequest request) {
		// 序号LABT|0
		if (key.equals("id")) {
			String b = "";
			if (jo != null) {
				if (jo.get("b") != null) {
					b = jo.getString("b");
				}
			} else {
				b = request.getParameter("b");
			}
			if (b.indexOf("|") != -1) {
				b = b.substring(b.indexOf("|") + 1);
			} else {
				b = "0";
			}
			return b;
			// Type LABT|0
		} else if (key.equals("type")) {
			String b = "";
			if (jo != null) {
				if (jo.get("b") != null) {
					b = jo.getString("b");
				}
			} else {
				b = request.getParameter("b");
			}
			if (b.indexOf("|") != -1) {
				b = b.substring(0, b.indexOf("|"));
			}
			return b;
		} else if (key.equals("t")) {
			if (jo != null) {
				if (jo.get("t") == null) {
					return System.currentTimeMillis() + "";
				}
				return jo.getString("t");
			}
			if (request.getParameter("t") == null) {
				return System.currentTimeMillis() + "";
			}
			return request.getParameter(key);
		}

		if (jo != null) {
			return jo.getString(key);
		} else {
			return request.getParameter(key);
		}

	}

	public EloUserDAO getEloUserDAO(HttpServletRequest request) {
		return (EloUserDAO) getBean(request, "eloUserDAO");
	}

	public BuildSettingXML getBuildSettingXML(HttpServletRequest request) {
		return (BuildSettingXML) getBean(request, "buildSettingXML");
	}

	public MemCachedDAO getMemCachedDAO(HttpServletRequest request) {
		return (MemCachedDAO) getBean(request, "memCachedDAO");
	}

	public Object getBean(HttpServletRequest request, String name) {
		ApplicationContext ctx = WebApplicationContextUtils
				.getRequiredWebApplicationContext(request.getSession()
						.getServletContext());
		return ctx.getBean(name);
	}

	public int getNeedGemFromGold(HttpServletRequest request, int gold) {
		int price = this.getBuildSettingXML(request).GlodPerGem;
		int need = (int)Math.ceil((gold*1D) / (price*1D));
		return need;
	}

	public int getNeedGemFromElxm(HttpServletRequest request, int elxm) {
		int price = this.getBuildSettingXML(request).ElxmPerGem;
		int need = (int)Math.ceil((elxm*1D) / (price*1D));
		return need;
	}

	public int getNeedGemFromDakm(HttpServletRequest request, int dakm) {
		int price = this.getBuildSettingXML(request).DakmPerGem;
		int need = (int)Math.ceil((dakm*1D) / (price*1D));
		return need;
	}

	public int getNeedGemFromMS(HttpServletRequest request, long ms) {
		int price = this.getBuildSettingXML(request).MSPerGem;
		int need = (int)Math.ceil((ms*1D) / (price*1D));
		return need;
	}
	
	
	private boolean CheckMove1(HttpServletRequest request, JSONObject ht,
			CcMap cm, int oldx, int oldy) {
		int x = Integer.parseInt(getParamValue("x", ht, request));
		int y = Integer.parseInt(getParamValue("y", ht, request));
		String type = getParamValue("type", ht, request);
		Building bu = getBuildSettingXML(request).findBuByID(type);
		if (bu == null) {
			return false;
		}
		int size = Integer.parseInt(bu.getSize());
//		Integer[][] map = cm.getMap();
		// 先取消旧建筑占位
	//	cm.setMap(RemoveMap(map, oldx, oldy, size));
	//	if (!CheckMap(map, x, y, size)) {
	//		return false;
	//	}
		// 检测通过后加入新建筑占位
	//	cm.setMap(SetMap(map, x, y, size));
		return true;
	}
	
	private boolean CheckMap1(Integer[][] map, int x, int y, int size) {
		for (int i = 0; i < size; i++) {
			for (int j = 0; j < size; j++) {
				if ((map[(x - 1 + i)][(y - 1 + j)] != null)
						&& (map[(x - 1 + i)][(y - 1 + j)].intValue() > 0)) {
					return false;
				}
			}
		}
		return true;
	}

	private Integer[][] SetMap1(Integer[][] map, int x, int y, int size) {
		for (int i = 0; i < size; i++) {
			for (int j = 0; j < size; j++) {
				map[(x - 1 + i)][(y - 1 + j)] = Integer.valueOf(1);
			}
		}
		return map;
	}

	private Integer[][] RemoveMap1(Integer[][] map, int x, int y, int size) {
		for (int i = 0; i < size; i++) {
			for (int j = 0; j < size; j++) {
				// 边缘还是设置成2
				if ((x - 1 + i) == 0 || (x - 1 + i) == 41 || (y - 1 + j) == 0
						|| (y - 1 + j) == 41) {
					map[(x - 1 + i)][(y - 1 + j)] = Integer.valueOf(2);
				} else {
					map[(x - 1 + i)][(y - 1 + j)] = Integer.valueOf(0);
				}

			}
		}
		return map;
	}
}
/*
 * Location: C:\Work\WorkSpace\classes\ Qualified Name:
 * com.coc.service.BaseService JD-Core Version: 0.6.2
 */